<template>
  <div class="messages-page">
    <Header />

    <div class="page-content">
      <!-- 面包屑导航 -->
      <div class="page-nav">
        <div class="breadcrumb">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/account' }">
              <i class="fas fa-chevron-left"></i> {{ $t('返回账户')}}
            </el-breadcrumb-item>
            <el-breadcrumb-item>{{ $t('我的消息')}}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
      </div>

      <!-- 主要内容 -->
      <div class="main-card">
        <h1 class="page-title">{{ $t('我的消息')}}</h1>

        <!-- 消息筛选 -->
        <div class="filter-bar">
          <div class="message-type-buttons">
            <el-button
              v-for="type in messageTypes"
              :key="type.value"
              :type="messageType === type.value ? 'primary' : 'default'"
              :plain="messageType !== type.value"
              @click="handleTypeChange(type.value)"
            >
              {{ type.label }}
            </el-button>
          </div>

          <div class="right-actions">
            <el-button
              type="primary"
              link
              :disabled="!hasUnread"
              @click="handleMarkAllRead"
            >
              {{ $t('全部标记为已读')}}
            </el-button>
          </div>
        </div>

        <!-- 消息列表 -->
        <div class="message-list">
          <div
            v-for="message in messages"
            :key="message.id"
            class="message-item"
            @click="handleMessageClick(message)"
          >
            <div class="message-content">
              <div class="message-title" :class="{ unread: !message.isRead }">
                {{ message.title }}
              </div>
              <div class="message-meta">
                <span class="message-time">{{ message.time }}</span>
                <span
                  class="message-type"
                  :class="message.type"
                >
                  {{ getTypeText(message.type) }}
                </span>
              </div>
            </div>
          </div>
        </div>

        <!-- 分页器 -->
        <div class="pagination-wrapper">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :total="total"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, sizes, prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <Footer />
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import Footer from '@/components/Footer.vue'
import {onMounted} from "@vue/runtime-core";
import {
  ApiNoticeList,
  ApiNoticeInfo,
  ApiNoticeRead,
  ApiNoticeIsRead
} from "@/api/notice.js";
import {useI18n} from "vue-i18n";
const { t } = useI18n();

const router = useRouter()

// 消息类型
const messageType = ref('system')

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const getMessageList = () =>{
  const params = {
    page: currentPage.value,
    pageSize: pageSize.value,
    type: 1,
    orderByColumn: 'createTime',
    isAsc: 'descending'
  }
  ApiNoticeList(params).then(res => {
    messages.value = res.rows
    total.value = res.total
  })
}

// 模拟消息数据
const messages = ref([])

// 是否有未读消息
const hasUnread = computed(() => {
  return messages.value.some(msg => !msg.isRead)
})

// 获取消息图标
const getMessageIcon = (type) => {
  switch (type) {
    case 'system':
      return 'fas fa-cog'
    case 'activity':
      return 'fas fa-gift'
    default:
      return 'fas fa-envelope'
  }
}

// 处理函数
const handleTypeChange = (value) => {
  messageType.value = value
  // TODO: 重新加载数据
}

const handleMarkAllRead = async () => {
  try {
    // TODO: 调用标记已读API
    await new Promise(resolve => setTimeout(resolve, 500))
    messages.value.forEach(msg => msg.isRead = true)
    ElMessage.success(t('已全部标记为已读'))
  } catch (error) {
    ElMessage.error(t('设置失败，请重试'))
  }
}

const handleMessageClick = (message) => {
  if (!message.isRead) {
    message.isRead = true
  }
  router.push(`/account/messages/${message.id}`)
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // TODO: 重新加载数据
  getMessageList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // TODO: 重新加载数据
  getMessageList()
}

const messageTypes = [
  // { value: 'all', label: t('全部消息')' },
  { value: 'system', label: t('系统通知') },
  { value: 'unread', label: t('未读消息') },
  // { value: 'activity', label: t('活动消息') }
]

const getTypeText = (type) => {
  switch (type) {
    case 'system':
      return t('系统通知')
    case 'activity':
      return t('活动消息')
    default:
      return t('未读消息')
  }
}

onMounted(() => {
  getMessageList()
})
</script>

<style lang="scss" scoped>
.messages-page {
  background-color: #f8f8f8;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.page-content {
  padding: 32px 24px;
  max-width: 1000px;
  margin: 0 auto;
  width: 100%;
  flex: 1;
}

.page-nav {
  margin-bottom: 24px;

  .breadcrumb {
    :deep(.el-breadcrumb__item) {
      .el-breadcrumb__inner {
        color: #565656;
        font-weight: normal;
        font-size: 14px;

        &:hover {
          color: #c45500;
        }

        i {
          margin-right: 4px;
          font-size: 12px;
        }
      }
    }
  }
}

.main-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  padding: 32px;
}

.page-title {
  font-size: 24px;
  color: #111;
  font-weight: 500;
  margin-bottom: 32px;
}

.filter-bar {
  margin-bottom: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  @media (max-width: 768px) {
    flex-direction: column;
    align-items: stretch;
    gap: 16px;
  }

  .message-type-buttons {
    display: grid;
    grid-template-columns: repeat(4, auto);
    gap: 12px;

    @media (max-width: 768px) {
      grid-template-columns: repeat(2, 1fr);
      gap: 8px;
    }

    :deep(.el-button) {
      margin: 0;
      justify-content: center;

      &.el-button--primary {
        background: #f0c14b;
        border-color: #a88734 #9c7e31 #846a29;
        color: #111;

        &:hover {
          background: #eeb933;
        }
      }

      &.el-button--default {
        border-color: #adb1b8 #a2a6ac #8d9096;
        color: #111;

        &:hover {
          background: #f7f8fa;
        }
      }
    }
  }

  .right-actions {
    display: flex;
    gap: 16px;
    align-items: center;

    @media (max-width: 768px) {
      justify-content: flex-end;
    }
  }
}

.message-list {
  .message-item {
    padding: 20px;
    border-bottom: 1px solid #eee;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: background-color 0.2s;

    &:hover {
      background-color: #fafafa;
    }

    @media (max-width: 768px) {
      padding: 16px;
      flex-direction: column;
      align-items: flex-start;
      gap: 8px;
    }

    .message-content {
      flex: 1;
      width: 100%;

      .message-title {
        font-size: 16px;
        color: #333;
        margin-bottom: 4px;

        &.unread {
          color: #111;
          font-weight: 500;

          &::before {
            content: '';
            display: inline-block;
            width: 6px;
            height: 6px;
            background: #f0c14b;
            border-radius: 50%;
            margin-right: 8px;
            vertical-align: middle;
          }
        }
      }
    }

    .message-meta {
      display: flex;
      align-items: center;
      gap: 16px;
      margin-top: 8px;
      color: #666;
      font-size: 14px;

      @media (max-width: 768px) {
        width: 100%;
        justify-content: space-between;
      }

      .message-type {
        padding: 2px 8px;
        border-radius: 12px;
        font-size: 12px;

        &.system {
          background: #e6f4ff;
          color: #0066c0;
        }

        &.activity {
          background: #f6ffed;
          color: #067d62;
        }
      }
    }
  }
}

.pagination-wrapper {
  margin-top: 24px;
  display: flex;
  justify-content: flex-end;
}
</style>
